/*
 * Lenovo Group
 * Copyright (c) 2015-2016 All Rights Reserved.
 * 
 * Project Name:lmrp-api
 * Create Time: 2016年2月19日 下午9:12:52
 */
package com.lenovo.lmrp.api.model.entity;

import java.io.Serializable;
import java.util.Date;
import java.util.List;
import java.util.Objects;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.persistence.Transient;

import com.lenovo.sdc.framework.util.CommonUtil;

/**
 * 
 * @author wujin
 * @version v1.0
 */
@Entity
@Table(name = "lmrp_ticket_process")
public class TicketProcessPO implements Serializable {
    /**  */
    private static final long serialVersionUID = 1L;

    public static enum ProcessInstStatus {
        STARTED,
        RUNNING,
        STOPED;
    }
    
    @Id
    @Column
    private String instanceId;

    @Column
    private String processId;

    @Column
    private String itsmCode;

    @Column
    private String ticketId;

    @Column
    @Enumerated(EnumType.ORDINAL)
    private ProcessInstStatus status;

    @Column
    private Date startTime;

    @Column
    private Date endTime;

    @Column
    private String activityKey;

    @Transient
    private List<ProcessActivityPO> activites;
    
    /**
     * generated after create process instance for a ticket
     * 
     * @return
     */
    public String getInstanceId() {
        return instanceId;
    }

    public void setInstanceId(String instanceId) {
        this.instanceId = instanceId;
    }

    public String getProcessId() {
        return processId;
    }

    public void setProcessId(String processId) {
        this.processId = processId;
    }

    public String getItsmCode() {
        return itsmCode;
    }

    public void setItsmCode(String itsmCode) {
        this.itsmCode = itsmCode;
    }

    public String getTicketId() {
        return ticketId;
    }

    public void setTicketId(String ticketId) {
        this.ticketId = ticketId;
    }

    public ProcessInstStatus getStatus() {
        return status;
    }

    public void setTStatus(ProcessInstStatus status) {
        this.status = status;
    }

    public Date getStartTime() {
        return startTime;
    }

    public void setStartTime(Date startTime) {
        this.startTime = startTime;
    }

    public Date getEndTime() {
        return endTime;
    }

    public void setEndTime(Date endTime) {
        this.endTime = endTime;
    }

    public String getActivityKey() {
        return activityKey;
    }

    public void setActivityKey(String activityKey) {
        this.activityKey = activityKey;
    }

    public List<ProcessActivityPO> getActivites() {
        return activites;
    }

    public void setActivites(List<ProcessActivityPO> activites) {
        this.activites = activites;
    }

    public void setStatus(ProcessInstStatus status) {
        this.status = status;
    }

    public ProcessActivityPO getCurrentActivity() {
        if (Objects.isNull(activites) || activites.isEmpty())
            return null;
        
        if (CommonUtil.isNullOrEmpty(activityKey))
            return null;
        
        for (ProcessActivityPO act : activites) {
            if (act.getActivityKey().endsWith(activityKey))
                return act;
        }
        
        return null;
    }
}
